# -*- coding: utf-8 -*-
"""
@Time ： 2024/1/27 0011 15:06
@Auth ： zpt
@File ：config_overview_page.py
@IDE ：PyCharm
"""
import time
from Page.base_page import BasePage
from Page.login_page import LoginPage
from Lib.readelement import Element
from Lib.log import log

config = Element('config_overview')


class Config_Overview_Page(BasePage):
    """系统资源页面"""
    cfg_name = "everything"
    cfg_repo = "https://repo.openeuler.org/openEuler-22.03-LTS/everything/"

    def login(self, url=None, name=None, password=None):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.login(url, name, password)

    def alert_message(self):
        """
        获取弹窗元素
        """
        self.mouse_move(config["assert"])
        alert = self.find_element(config["assert"])

        return alert.text

    def check_elements(self, eles_list):
        """
         页面信息查询
        """
        try:
            flag = True
            for ele in eles_list:
                log.info('check element ' + ele)
                flag = self.is_element_exist(config[ele])
                if not flag:
                    break
            return flag
        except Exception as e:
            log.error('check element ' + ele + ' failed' + 'flag:' + str(flag), e)

    def enter_config_view(self):
        """
        进入config页面
        """
        try:
            self.click(config['config'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found config button timeout")
            log.info(e)

    def add_target_view(self):
        self.enter_config_view()
        time.sleep(1)
        self.click(config['add_build_target'])
        time.sleep(2)

    def click_add_build_target(self):
        """
        点你添加构建目标
         """
        try:
            self.click(config['add_build_target'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found add build target button timeout")
            log.info(e)

    def add_target_os_variant_null(self):
        self.enter_config_view()
        time.sleep(1)
        self.click_add_build_target()
        time.sleep(2)
        self.click(config['arch'])
        time.sleep(2)
        self.click(config['choose_arch'])
        time.sleep(2)
        self.click(config['os_variant_confirm'])

    def add_target_arch_null(self):
        self.enter_config_view()
        time.sleep(1)
        self.click_add_build_target()
        time.sleep(2)
        self.click(config['os_variant'])
        time.sleep(2)
        self.click(config['choose_variant'])
        time.sleep(2)
        self.click(config['os_variant_confirm'])

    def add_build_target_close_flag(self):
        self.enter_config_view()
        time.sleep(1)
        self.click_add_build_target()
        time.sleep(2)
        self.click(config['os_variant'])
        time.sleep(2)
        self.click(config['choose_variant'])
        time.sleep(2)
        self.click(config['arch'])
        time.sleep(2)
        self.click(config['choose_arch'])
        time.sleep(2)
        self.click(config['build_flag'])
        time.sleep(3)
        self.click(config['publish_flag'])
        time.sleep(3)
        self.click(config['os_variant_confirm'])

    def add_build_target(self):
        self.enter_config_view()
        time.sleep(1)
        self.click_add_build_target()
        time.sleep(2)
        self.click(config['os_variant'])
        time.sleep(2)
        self.click(config['choose_variant'])
        time.sleep(2)
        self.click(config['arch'])
        time.sleep(2)
        self.click(config['choose_arch'])
        time.sleep(2)
        self.click(config['os_variant_confirm'])

    def edit_build_target(self):
        """
        点击构建目标的编辑按钮
        """
        try:
            self.click(config['build_target_edit'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found build target edit timeout")
            log.info(e)

    def save_build_target(self):
        """
        点击构建目标的保存按钮
        """
        try:
            self.click(config['build_target_save'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found build target save timeout")
            log.info(e)

    def delete_build_target(self):
        """
        点击构建目标的删除按钮
        """
        try:
            self.click(config['build_target_delete'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found build target delete timeout")
            log.info(e)

    def cancel_build_target(self):
        """
        点击构建目标的取消按钮
        """
        try:
            self.click(config['build_target_cancel'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found build target cancel timeout")
            log.info(e)

    def edit_ground_project(self):
        """
        修改地基工程
        """
        self.edit_build_target()
        time.sleep(2)
        self.click(config['edit_ground_project'])
        time.sleep(2)
        self.click(config['choose_ground_project'])

    def build_target_delete_confirm(self):
        """
         点击删除构建目标的确认按钮
         """
        try:
            self.click(config['target_delete_confirm'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found build target delete confirm button timeout")
            log.info(e)

    def build_target_delete_cancel(self):
        """
         点击删除构建目标的取消按钮
         """
        try:
            self.click(config['target_delete_cancel'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found build target delete cancel button timeout")
            log.info(e)

    def add_build_target_close(self):
        self.add_target_view()
        try:
            self.click(config['add_build_target_close'])
        except Exception as e:
            log.info("error: found add build target  close button timeout")
            log.info(e)

    def build_target_delete_close(self):
        """
         点击删除构建目标的关闭按钮
         """
        try:
            self.click(config['target_delete_close'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found build target delete close button timeout")
            log.info(e)

    def add_repo_view(self):
        self.enter_config_view()
        time.sleep(1)
        self.click(config['add_repo'])
        time.sleep(2)

    def add_repo(self):
        self.enter_config_view()
        time.sleep(1)
        self.click(config['add_repo'])
        time.sleep(2)
        self.send_keys(config['repo_name'], self.cfg_name)
        time.sleep(2)
        self.send_keys(config['repo_address'], self.cfg_repo)
        time.sleep(2)
        self.click(config['repo_confirm'])

    def add_repo_name_null(self):
        self.enter_config_view()
        time.sleep(1)
        self.click(config['add_repo'])
        time.sleep(2)
        self.send_keys(config['repo_address'], self.cfg_repo)
        time.sleep(2)
        self.click(config['repo_confirm'])

    def add_repo_address_null(self):
        self.enter_config_view()
        time.sleep(2)
        self.click(config['add_repo'])
        time.sleep(2)
        self.send_keys(config['repo_name'], self.cfg_name)
        time.sleep(2)
        self.click(config['repo_confirm'])

    def add_repo_address_confirm(self):
        self.enter_config_view()
        time.sleep(2)
        self.click(config['add_repo'])
        time.sleep(2)
        self.send_keys(config['repo_name'], self.cfg_name)
        time.sleep(2)

    def add_rpm_repo_close(self):
        """
         点击添加repo源的关闭按钮
         """
        try:
            self.click(config['rpm_repo_close'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found add rpm repo close button timeout")
            log.info(e)

    def add_rpm_repo_cancel(self):
        """
         点击删除构建目标的取消按钮
         """
        try:
            self.click(config['rpm_repo_cancel'])
            time.sleep(1)
        except Exception as e:
            log.info("error: found add rpm repo cancel button timeout")
            log.info(e)

    def add_repo_cancel(self):
        self.enter_config_view()
        time.sleep(1)
        self.click(config['add_repo'])
        time.sleep(2)
        self.add_rpm_repo_cancel()

    def add_repo_close(self):
        self.enter_config_view()
        time.sleep(1)
        self.click(config['add_repo'])
        time.sleep(2)
        self.add_rpm_repo_close()

    def my_repo_text(self):
        repo_name = self.get_element_text(config['my_repo_name'])
        repo_address = self.get_element_text(config['my_repo_address'])

        return [repo_name, repo_address]

    def build_target_text(self):
        os_variant_text = self.get_element_text(config['my_os_variant'])
        arch_text = self.get_element_text(config['my_arch'])

        return [os_variant_text, arch_text]

    def input_config_text(self):
        self.enter_config_view()
        time.sleep(1)
        self.send_keys(config['config_input'], 'squid_enable: n')
        time.sleep(1)
        self.click(config['user_page'])
        message = self.alert_message()
        return message

    def delete_rpm_repo_cancel(self):
        self.add_repo()
        time.sleep(2)
        self.click(config['rpm_repo_delete'])
        time.sleep(1)
        self.click(config['repo_delete_cancel'])
        time.sleep(1)

    def delete_rpm_repo_close(self):
        self.add_repo()
        time.sleep(2)
        self.click(config['rpm_repo_delete'])
        time.sleep(1)
        self.click(config['repo_delete_close'])
        time.sleep(1)

    def delete_rpm_repo_confirm(self):
        self.add_repo()
        time.sleep(2)
        self.click(config['rpm_repo_delete'])
        time.sleep(1)
        self.click(config['repo_delete_confirm'])
        time.sleep(1)
